home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / RECORDS.SWG / 0004_RECINFO1.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  8KB  |  213 lines

  1. >Can anyone tell me how I can delete a Record from a File?
  2.  
  3. Two ways.
  4.  
  5. 1) Mark the Record With a value that tells your Program that
  6.    Record is classified as deleted. (Most DB Programs work this
  7.    way)
  8.  
  9. 2) Read the WHOLE File in, one Record at a time. Copy these
  10.    Records into the new File. While you are copying, ignore any
  11.    Record(s) you want to delete.
  12.  
  13.    Once the copy is Complete, delete the old File and rename the
  14.    new to the old Filename...
  15.  
  16. You could also open the File as unTyped :-), and use blockRead to read big
  17. chunks Until you've read (recSize * number of Records beFore bad one) Bytes,
  18. and blockWrite to Write them out to the temp File.  This might be faster,
  19. depending on the number of Records and how big they are.
  20.  
  21. > How would I pack a File of Records to save space?
  22.  
  23.     if you have seen other posts by me about packing Database Type
  24. Files, you will see I normally recommend against it.
  25.  
  26. I usually go With a "Deleted" indicator and Records marked as
  27. deleted are re-used. It is my personnal experience that such Files
  28. normally grow in size Until they reach a balanced state. A state in
  29. which the File does not grow anymore and where balance is reached
  30. between Additions and Deletions.
  31.  
  32. if you Really want to pack it then,
  33.  
  34.     1- Build a list of deleted Records
  35.     2- take the last Record in the File
  36.     3- Keep a note that that Record is now empty
  37.     4- OverWrite the FIRST DELETED Record's space With it
  38.     5- Repeat steps 2-4 Until all deleted Records have been filled.
  39.     6- Seek to the first Record marked empty, this should be the last
  40.        one you moved.
  41.        or you could simply seek to Record_COunt since the File should
  42.        now contain no empty space all valid Records should be at the
  43.        head of the File and thus seeking to Record_Count ensures you
  44.        of being positionned at the very end.
  45.     7- Truncate the File there by using the Truncate command.
  46.     8- Be sure to close and re-open the File beFore any more processing
  47.        so that Directory and FAT are updated.
  48.  
  49.  
  50. > Could someone please give me an example of a good way (the best way?) to
  51. > delete Records from a Typed File?  My only guess, after looking at all
  52. > of the TP documentation, and my one TP book, is that I will have to read
  53. > the File, and Write a new File, excluding the "deleted" Record.  This
  54. > will require another (identical) Type statment, and File Variable, etc.,
  55. > and will require me to then rename the smaller File...
  56. >
  57. > Isn't there a more efficient way????
  58.  
  59. YES THERE IS!!!
  60.  
  61. Modify your Record to this:
  62.  
  63. Type
  64.   My_Record = Record
  65.     Deleted : Boolean;
  66.     < Rest of your usual stuff >
  67.   end;
  68.  
  69.     Now if you use index Files, create an index File With the DELETED
  70. field as the key to that index.
  71.  
  72. Pseudo code ahead:
  73.  
  74.     Adding a Record:
  75.         1- Set index to DELETED
  76.         2- Go to first Record
  77.         3- Is it deleted
  78.         4- Yes, use it as our new Record
  79.             4a Change the Deleted field to False;
  80.             4b Write our new Record to this one overwriting it.
  81.         5- No, Add a new phisical Record to the File,
  82.             5a Remember to Set Deleted to False on this one
  83.             5b Write our Record to the new Record.
  84.  
  85.     Deleting a Record:
  86.         1- Go to Record,
  87.         2- Read it in
  88.         3- Set the deleted field to True.
  89.             { This allows UnDeleting a Record With it's original data}
  90.  
  91.     Listing Records:
  92.         Remember to skip deleted Records.
  93.  
  94. you will soon find that using this management method your applications
  95. will perForm at speeds vastly faster than others. Simply because the
  96. File is never moved, truncated etc.. Eliminating fragentation on the
  97. disk. You will also find that as you open new databases, they will
  98. quickly grow and then attain a stable size where new Records are mostly
  99. reusing deleted Records.
  100.  
  101.     This is how my dBase applications are handled. Our last project
  102. While I was at the Justice Departement was re-written to use this
  103. principle of management instead of using dBase's Pack and delete
  104. routines. The efficiency was greatly augmented both in speed and in disk
  105. occupation. We no longer needed to perForm unfragmentation routines
  106. periodically and we also could reverse any error our users might have
  107. commited.
  108.  
  109.     By adding additionnal info such as deletion date, user ID that
  110. requested the delete etc... we were able to offer options that were not
  111. available beFore. An added benefit was that we didn't need to reindex
  112. the whole database. Affected Index Files were open during operations and
  113. were thus opdated on the fly. So our Deleted index was allways uptodate.
  114. Generating a message when physical space was added to the database, we
  115. were able to perForm defragementation only when Really needed. and those
  116. operations were greatly shortened in time because 98% of the database
  117. was allready defragmentated.
  118.  
  119. It's the sensible way to do it, and it can be done in any language.
  120.  
  121. {------------------------------------------------------------------------}
  122.  
  123. >   Can someone tell me how to search through a Typed File.
  124. >   Example:
  125. >      Type Dummy = Record
  126. >                     Name : String
  127. >                     Age  : Integer
  128. >                   end;
  129. >           DummyFile = File of Dummy;
  130.  
  131. >   How could I find a Record that has AGE = 20 ?
  132.  
  133. Do something like this:
  134.  
  135. Var
  136.   PersonRecord : Dummy;
  137.  
  138. Procedure searchForAge(PersonsAge: Integer);
  139. begin
  140.    ... Open the File ...
  141.    Seek(DummyFile, 0);        {start at beginning of File}
  142.    PersonRecord.Age:= 1000;   {Init to unused value}
  143.    While not(Eof) and (PersonRecord.Age <> PersonsAge) do
  144.    begin
  145.        Read(DummyFile, PersonRecord);
  146.    end;
  147.    ... Close the File ...
  148. end;
  149.  
  150.  
  151. This might work:
  152.  Type
  153.    rec = Record
  154.      age: Integer;
  155.      etc...
  156.    end;
  157.  Var
  158.    f: File of rec;
  159.    r: rec;
  160.  begin
  161.    assign(f, 'FileNAME.EXT');
  162.    reset(f, sizeof(rec));           (* have to indicate Record size *)
  163.  
  164.    While not eof(f) do begin
  165.      blockread(f, r, 1);            (* read one rec from f into r *)
  166.      if r.age = 20 then
  167.        (* do something interesting *)
  168.    end;
  169.    close(f);
  170.  end.
  171.  
  172. The trick is to inForm the runtime library of the size of each Record in the
  173. File; you do this in the call to reset.  Blockread takes a File, an unTyped
  174. buffer, and the number of Records to read.
  175.  
  176.  
  177.  
  178.  
  179.  
  180. >I know that this is probably as common as the "I want to Write a BBS",
  181. >and "*.MSG structures" :) but haven't seen anything lately (and I've
  182. >been reading this For quite some time) about tips on searching Records
  183. >in a File.  Any tips/routines on fast searching, or ways to set up the
  184. >Record For fast searching?  Thanks.
  185.  
  186.    Well, you're kinda restricting yourself by saying, "in a File".  That means
  187. you have a File of some Type, and you're pretty-much confined to the ways
  188. appropriate For that File Type.  However, there are some things you can do
  189. which will make processing the data faster and more efficient:
  190.  1. For Text Files:
  191.    a. use large buffers (SetTextBuf Procedure)
  192.    b. establish an Index For the Records on the File, and use random i/o to
  193. access specific Records.  Thgis does not imply reading all the Records each
  194. time you "search it", but you must have some "key" or order in that File from
  195. which you can assign and index key.  This is a large subject, and to go
  196. further, I'd have to know more about your File.
  197.    c. have the File (be of) fixed Records, sort on some key field, and use a
  198. binary search/random read scheme to search For Records.  Also pretty
  199. Complicated in Implementation...
  200.   2. Random Files:
  201.     There are many options here, and are mostly dependant on the File order and
  202. data content/Record size.
  203.  
  204.    Finally,  suggest you read the entire File into memory (using Heap, Pointer
  205. access, etc.), and do all your work in memory.  The above ideas are appropriate
  206. For very large Files, but anything under 450K can be stored in you system
  207. memory and used there, I'll bet.  Once in memory, you can even sort the data in
  208. some fashion and use some fancy searching techniques, too.
  209.  
  210.  
  211.  
  212.  
  213.